package role

import (
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role/team"
	"sanguo/node/node_game/user"
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/getRoleInfo"
	"sanguo/rpc/teamToGameMsg"

	"github.com/golang/protobuf/proto"
)

const (
	status_playing = 4 //正常游戏中
)

type TeamToGameMsg struct {
}

func (this *TeamToGameMsg) OnCall(replyer *teamToGameMsg.TeamToGameMsgReplyer, arg *ss_rpc.TeamToGameMsgReq) {

	node_game.Debugln("OnCall TeamToGameMsg", arg)

	resp := &ss_rpc.TeamToGameMsgResp{
		Ok: proto.Int32(0),
	}

	if arg.GetMsgType() == ss_rpc.TTGType_Join_Team { //加入队伍
		_user := user.GetUser(arg.GetUserID())
		if _user != nil && _user.CcheckStatus(status_playing) {
			_roleModule := _user.GetModuleByName("role").(*RoleModule)
			_role := _roleModule.GetRole(arg.GetRoleID())
			if _role == _roleModule.CurrentRole() { //角色是否是当前的角色
				if _role != nil && _role.IsRoleModleData() && _role.Team == nil { //非 全部处理为加入其它队伍
					_team := &team.Team{
						TeamID:     arg.GetTeamID(),
						TeamHeader: false,
					}
					_role.Team = _team

					resp.Ok = proto.Int32(1)
					resp.UserID = proto.String(_role.GetUserID())
					resp.RoleID = proto.Uint64(_role.GetID())
					resp.Name = proto.String(_role.GetName())
					resp.Level = proto.Int32(_role.GetLev())
					resp.Portrait = proto.Int32(_role.GetPortrait())
					resp.Face = proto.Int32(_role.GetFace())
					resp.Hair = proto.Int32(_role.GetHair())
					resp.HairColor = proto.Int32(_role.GetHairColor())
					resp.EyeColor = proto.Int32(_role.GetEyeColor())
					resp.Cloth = proto.Int32(_role.GetCloth())
					resp.ClothColor = proto.Int32(_role.GetClothColor())
					resp.Outline = proto.Int32(_role.GetOutline())
					resp.Weapon1 = proto.Int32(_role.GetWeapon1())
					resp.Game = proto.String(node_game.GetSelfService().ToPeerID().ToString())

				} else {
					resp.RespType = ss_rpc.TTGRespType(ss_rpc.TTGRespType_Join_Other_Team).Enum()
				}
			} else {
				resp.RespType = ss_rpc.TTGRespType(ss_rpc.TTGRespType_Off_Line).Enum()
			}

		} else {
			resp.RespType = ss_rpc.TTGRespType(ss_rpc.TTGRespType_Off_Line).Enum()
		}
	} else { //更换队长 踢人
		_user := user.GetUser(arg.GetUserID())
		if _user != nil {
			_roleModule := _user.GetModuleByName("role").(*RoleModule)
			_role := _roleModule.GetRole(arg.GetRoleID())
			if _role != nil && _role.IsRoleModleData() {
				if arg.GetMsgType() == ss_rpc.TTGType_Team_Header {
					if _role.Team != nil {
						if arg.GetHeaderID() == _role.GetID() {
							_role.Team.TeamHeader = true
						} else {
							_role.Team.TeamHeader = false
						}
						resp.Ok = proto.Int32(1)
					}
				} else if arg.GetMsgType() == ss_rpc.TTGType_Kick_Role {
					_role.Team = nil
					resp.Ok = proto.Int32(1)
				}
			}
		}
	}

	replyer.Reply(resp)

}

type GetRoleInfo struct {
}

func (this *GetRoleInfo) OnCall(replyer *getRoleInfo.GetRoleInfoReplyer, arg *ss_rpc.GetRoleInfoReq) {

	node_game.Debugln("OnCall GetRoleInfo", arg)

	resp := &ss_rpc.GetRoleInfoResp{
		Ok: proto.Bool(false),
	}

	_user := user.GetUser(arg.GetUserID())
	if _user != nil && _user.CcheckStatus(status_playing) {
		_roleModule := _user.GetModuleByName("role").(*RoleModule)
		r := _roleModule.CurrentRole()
		if r != nil && r.IsRoleModleData() {
			_role := &ss_rpc.RoleInfo{
				UserID:     proto.String(r.GetUserID()),
				RoleID:     proto.Uint64(r.GetID()),
				Name:       proto.String(r.GetName()),
				Level:      proto.Int32(r.GetLev()),
				Portrait:   proto.Int32(r.GetPortrait()),
				Outline:    proto.Int32(r.GetOutline()),
				Face:       proto.Int32(r.GetFace()),
				Hair:       proto.Int32(r.GetHair()),
				HairColor:  proto.Int32(r.GetHairColor()),
				EyeColor:   proto.Int32(r.GetEyeColor()),
				Cloth:      proto.Int32(r.GetCloth()),
				ClothColor: proto.Int32(r.GetClothColor()),
				Weapon1:    proto.Int32(r.GetWeapon1()),
				Weapon2:    proto.Int32(r.GetWeapon2()),
			}

			for _, b := range r.GetRoleBattleAttr().GetBattleAttrList() {
				_role.BattleAttrs = append(_role.BattleAttrs, &ss_rpc.Attribute{
					Index: proto.Int32(b.Idx),
					Value: proto.Int32(b.Val),
				})
			}

			/*
				for _, s := range r.GetRoleSkill(). {
					_role.Skills = append(_role.Skills, &ss_rpc.Skill{
						SkillID:     proto.Int32(s.SkillID),
						SkillLevel:  proto.Int32(s.SkillLevel),
						SkillType:   proto.Int32(s.SkillType),
						SpecialStar: proto.Int32(s.SpecialStar),
					})
				}
			*/
			resp.Role = _role
			resp.Ok = proto.Bool(true)
		}
	}

	replyer.Reply(resp)

}

func init() {
	teamToGameMsg.Register(&TeamToGameMsg{})
	getRoleInfo.Register(&GetRoleInfo{})
}
